package com.maiji.cloud.controller;

import com.google.common.collect.Lists;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.List;

public class BaseController {

    protected static final String REST_URL_APPLOGIN = "http://MAIJICLOUD-APPLOGIN";
    protected static final String REST_URL_SHOPINGMALL = "http://MAIJICLOUD-SHOPINGMALL";
    protected static final String REST_URL_PREFIX_COMMUNITY = "http://MAIJICLOUD-COMMUNITY";

    @Autowired
    protected RestTemplate restTemplate;

    public <T, E> T postForObjectWithToken (String token, String url, E reqDto, Class<T> result) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("maijiToken", token);
        HttpEntity<E> httpEntity = new HttpEntity<>(reqDto, headers);
        return restTemplate.postForObject(url, httpEntity, result);
    }

    public <T> T postForObjectWithMultipart (String token, MultipartFile uploadFile, String url, Class<T> result) throws IOException {
        File localFile = null;
        try {
            MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
            HttpHeaders headers = new HttpHeaders();
            headers.add("maijiToken", token);
            headers.setContentType(MediaType.parseMediaType("multipart/form-data"));
            localFile = new File(uploadFile.getOriginalFilename());
            FileUtils.copyInputStreamToFile(uploadFile.getInputStream(), localFile);
            param.add("uploadFile", new FileSystemResource(localFile));
            HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(param, headers);
            return restTemplate.postForObject(url, httpEntity, result);
        } finally {
            if (localFile != null && localFile.exists()) localFile.delete();
        }
    }

    public <T> T postForObjectWithMultiparts (String token, String bucketName, MultipartFile[] uploadFiles, String url, Class<T> result) throws IOException {
        List<File> localFiles = Lists.newArrayList();
        try {
            MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
            HttpHeaders headers = new HttpHeaders();
            headers.add("maijiToken", token);
            headers.add("bucketName", bucketName);
            headers.setContentType(MediaType.parseMediaType("multipart/form-data"));
            for (MultipartFile uploadFile: uploadFiles) {
                File localFile = new File(uploadFile.getOriginalFilename());
                FileUtils.copyInputStreamToFile(uploadFile.getInputStream(), localFile);
                param.add("uploadFiles", new FileSystemResource(localFile));
                localFiles.add(localFile);
            }
            HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(param, headers);
            return restTemplate.postForObject(url, httpEntity, result);
        } finally {
            if (localFiles.size() != 0) {
                for (File localFile : localFiles)
                    if (localFile.exists()) localFile.delete();
            }
        }
    }

}
